import 'package:flutter/material.dart';
import 'package:rainflutter/config/define.dart';
import 'package:rainflutter/main.dart';
import 'package:rainflutter/pages/controller/page/provide/inherited_change_test.dart';

class StatefulBuilderTest extends StatefulWidget {
  final Map arguments;

  const StatefulBuilderTest({Key key, this.arguments}) : super(key: key);
  @override
  _StatefulBuilderTestState createState() => _StatefulBuilderTestState();
}

class _StatefulBuilderTestState extends State<StatefulBuilderTest>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  StateSetter _stateSetter1;

  StateSetter _stateSetter2;

  String name = '小王';

  int age = 10;

  String gender = '男';

  String like = '篮球';

  Color color = Colors.red;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
    if (widget?.arguments != null) {
      widget.arguments['names'] = ['1', '2'];
      widget.arguments['name'] = '';
      List a = widget.arguments['names'];
      String b = widget.arguments['name'];
      print(widget.arguments['names']);
      print(widget.arguments['name']);

      // a.clear();
      // b = 'null';
    }
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('StatefulBuilder')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ImageBox(),
            StlessBox(),
            StatefulBuilder(
                builder: (BuildContext context, StateSetter stateSetter) {
              _stateSetter1 = stateSetter;
              return Text('姓名: $name');
            }),
            StatefulBuilder(
                builder: (BuildContext context, StateSetter stateSetter) {
              _stateSetter2 = stateSetter;
              return Text('年龄: $age');
            }),
            GanderBox(key: appDefine.genderKey, gander: gender),
            LikeBox(UniqueKey(), like),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FlatButton(
                    onPressed: () {
                      _updateSource();
                      _stateSetter1(() {
                        print('刷新完了_stateSetter1');
                      });
                    },
                    child: Text('更新名字')),
                TextButton(
                    style: ButtonStyle(
                      textStyle:
                          MaterialStateProperty.all(TextStyle(fontSize: 14)),
                      backgroundColor: MaterialStateProperty.all(Colors.pink),
                      minimumSize: MaterialStateProperty.all(Size(100, 44)),
                    ),
                    onPressed: () {
                      _updateSource();
                      _stateSetter2(() {
                        print('刷新完了_stateSetter2');
                      });
                    },
                    child: Column(
                      children: [
                        Image.asset(
                          'assets/images/calendar_winter.png',
                          width: 20,
                          height: 10,
                          // fit: BoxFit.fill,
                        ),
                        Text('更新年龄'),
                      ],
                    )),
                TextButton(
                    onPressed: () {
                      _updateSource();

                      print('更新了性别');

                      GanderBox btn =
                          appDefine.genderKey.currentWidget as GanderBox;

                      appDefine.genderKey.currentState.setState(() {
                        btn.gander = gender;

                        print('更新完成');
                      });

                      color = Colors.amber;

                      print(appDefine.genderKey.currentState);
                    },
                    child: Text('更新性别')),
                TextButton(
                    onPressed: () {
                      _updateSource();
                      setState(() {
                        print('一起更新了setState');
                      });
                    },
                    child: Text('都更新'))
              ],
            ),
          ],
        ),
      ),
    );
  }

  void _updateSource() {
    name = name == '老张' ? '老王' : '老张';
    age++;
    gender = gender == '男' ? '女' : '男';
  }
}

// ignore: must_be_immutable
class GanderBox extends StatefulWidget {
  String gander;

  GanderBox({Key key, this.gander: '男'}) : super(key: key);
  @override
  _GanderBoxState createState() => _GanderBoxState();
}

class _GanderBoxState extends State<GanderBox> {
  @override
  Widget build(BuildContext context) {
    print('GanderBox-build');
    return Container(
        color: widget.gander == '男' ? Colors.blue : Colors.pink,
        child: Text('性别: ${widget.gander}'));
  }
}

class LikeBox extends StatefulWidget {
  final String likeSport;

  const LikeBox([Key key, this.likeSport]) : super(key: key);
  @override
  _LikeBoxState createState() => _LikeBoxState();
}

class _LikeBoxState extends State<LikeBox> {
  @override
  Widget build(BuildContext context) {
    print('LikeBox-build');
    String like = widget.likeSport;
    return TextButton(
        onPressed: () {
          setState(() {
            like = like == '篮球' ? '足球' : '篮球';
            print('更新喜欢的东西 $like');
          });
        },
        child: Text(like));
  }
}
